package com.liruo.learn.spring.mvc.controller.m1model;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author:liruo
 * @Date:2023-10-05-22:27:16
 * @Desc
 */
@Controller
public class ModelAndViewTest {

  @GetMapping("/ModelAndView")
  public ModelAndView modelTest(Model model, ModelMap modelMap, HttpSession session,
      HttpServletRequest req) {
    String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    model.addAttribute("model", "model-" + format);
    modelMap.addAttribute("modelMap", "modelMap-" + format);
    req.setAttribute("req", "req-" + format);
    session.setAttribute("session", "session-" + format);
    req.getServletContext().setAttribute("servletContext", "ServletContext-" + format);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("model");
    modelAndView.addObject("modelAndView", "modelAndView-" + format);
    return modelAndView;
  }
}
